import { Body, Controller, Get, Param, ParseIntPipe, Patch, UseGuards } from "@nestjs/common";
import { ApiOkResponse, ApiOperation, ApiTags } from "@nestjs/swagger";
import { RoleAccessService } from "src/service/role-access/role-access.service";
import { RoleAccessReqDto } from "./dto/role.access.req.dto";
import { RoleAccessVo } from "./vo/role.access.res";
import { AuthGuard } from '@nestjs/passport';
@ApiTags('后台管理系统-角色资源管理')
@UseGuards(AuthGuard('jwt'))
@Controller('role_access')
export class RoleAccessController {
  constructor(private readonly roleAccessService: RoleAccessService) {}

  @ApiOperation({
    summary: '给角色分配菜单资源',
    description: '根据角色ID给当前角色分配菜单或接口资源',
  })
  @ApiOkResponse({
    type: String,
    description: '给当前角色分配菜单或接口资源返回值',
  })
  @Patch('/:roleId')
  async roleToAccess(
    @Param('roleId', new ParseIntPipe()) roleId: number,
    @Body() roleAccessReqDto: RoleAccessReqDto,
  ): Promise<string> {
    return await this.roleAccessService.roleToAccess(roleId, roleAccessReqDto);
  }


  @ApiOperation({
    summary: '获取资源',
    description: '根据角色ID获取已经分配的菜单或接口',
  })
  @ApiOkResponse({
    type: RoleAccessVo,
    isArray: true,
    description: '根据角色ID返回授权的资源列表',
  })
  @Get('/:roleId')
  async accessListByRoleId(
    @Param('roleId', new ParseIntPipe()) roleId: number,
  ): Promise<RoleAccessVo[]> {
    return await this.roleAccessService.accessListByRoleId(roleId);
  }
}
